package com.itboye.sunsun.person.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.itboye.sunsun.adapter.AbstractOrderAdapter;
import com.itboye.sunsun.adapter.WaitPayOrderAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderBean;
import com.itboye.sunsun.beans.OrderBean.DetailBean;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.DoubleParse;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

public class AllFragment extends BaseOrderFragment {

	//全部状态码
	String status="0";
	private List<InnerDetailBean>dataList=new ArrayList<OrderBean.InnerDetailBean>();
	private Map<String, DetailBean>map;
	
	@Override
	protected String onGetStatus() {
		return status;
	}
	@Override
	protected BaseAdapter onGetAdapter() {
		return  new WaitPayOrderAdapter(App.ctx	, dataList); 
	}
	@Override
	protected void onPullDownFinish(BaseAdapter adapter, XListView xListView, OrderBean orderBean) {
		dataList.clear();
		addDataToList(dataList,orderBean);
		adapter.notifyDataSetChanged();
	}
	@Override
	
	protected void onPullUpFinish(BaseAdapter adapter, XListView xListView, OrderBean orderBean) {
		addDataToList(dataList,orderBean);
		adapter.notifyDataSetChanged();
	}



	@Override
	protected void onPullDownError(BaseAdapter adapter, XListView xListView, Exception exception, int code,
			String msg) { }

	@Override
	protected void onPullUpError(BaseAdapter adapter, XListView xListView, Exception exception, int code, String msg) {
		 }
	
	private void addDataToList(List<InnerDetailBean> dataList, OrderBean orderBean) {
		map= orderBean.getList();
		if (map==null) {
			return;
		}
		Set<Entry<String, DetailBean>> set=map.entrySet();
		if (set==null) {
			return;
		}
		for (Entry<String, DetailBean> entry : set) {
			
			int count=0;
			double price=0;
			if (entry==null||entry.getValue()==null||entry.getKey()==null) {
				continue;
			}
			int paystatus=Integer.parseInt(entry.getValue().getPayStatus());
			int orderstatus=Integer.parseInt(entry.getValue().getOrderStatus());
			
			//订单的四种状态
			if((paystatus==1&&orderstatus==2)||(paystatus==1&&orderstatus==5)||(paystatus==1&&orderstatus==3)||(paystatus==1&&orderstatus==4)||(paystatus==1&&orderstatus==7)||(orderstatus==2&&paystatus==0)||(paystatus==0&&orderstatus==8))
			{
			
			
			
			List<InnerDetailBean>  list=entry.getValue().get_items();
			if (list==null) {
				continue;
			}
			for (InnerDetailBean innerDetailBean : list) {
				dataList.add(innerDetailBean);
				int c=(int) DoubleParse.parse(innerDetailBean.getCount(), 0);
				count+=c;
				price=price+DoubleParse.parse(innerDetailBean.getPrice(),0)*c;
			}
			
			
			
			//int sta=Integer.parseInt(entry.getValue().getOrderStatus());
			
			//DebugLog.v("status",""+ sta);
			//采用待发货界面
			if((paystatus==1&&orderstatus==3)||(paystatus==1&&orderstatus==2))
			{
				InnerDetailBean detailBean=new InnerDetailBean();
				detailBean.setViewType(2);
				detailBean.setOrderCode(entry.getKey()); 
				detailBean.setCount(count+"");
				detailBean.setPrice(entry.getValue().getPrice());
				//设置订单ID
				detailBean.setPostPrice(""+entry.getValue().getPostPrice());
				detailBean.setDiscountRadio(""+entry.getValue().getDiscountMoney());
				detailBean.setId(entry.getValue().getId());
				dataList.add(detailBean);
			}//采用待付款界面
			else if(paystatus==0&&orderstatus==2)
			{
				InnerDetailBean detailBean=new InnerDetailBean();
				detailBean.setViewType(1);
				detailBean.setOrderCode(entry.getKey()); 
				detailBean.setCount(count+"");
				
				detailBean.setPrice(""+entry.getValue().getPrice());
				//Log.v("postprice",entry.getValue().getPostPrice());
				detailBean.setPostPrice(""+entry.getValue().getPostPrice());
				detailBean.setDiscountRadio(""+entry.getValue().getDiscountMoney());
				//设置订单ID
				detailBean.setId(entry.getValue().getId());
				dataList.add(detailBean);
		   	}
			//采用待收货界面
			else if(paystatus==1&&orderstatus==4)
		   	{
				InnerDetailBean detailBean=new InnerDetailBean();
				detailBean.setViewType(3);
				detailBean.setOrderCode(entry.getKey()); 
				detailBean.setCount(count+"");
				detailBean.setPrice(entry.getValue().getPrice());
				//设置邮费
				detailBean.setPostPrice(""+entry.getValue().getPostPrice());
				detailBean.setDiscountRadio(""+entry.getValue().getDiscountMoney());
				//设置订单ID
				detailBean.setId(entry.getValue().getId());
				dataList.add(detailBean);
		   	}
			//采用已完成界面
			else if(paystatus==1&&orderstatus==7||paystatus==1&&orderstatus==5)
			{
				
				InnerDetailBean detailBean=new InnerDetailBean();
				detailBean.setViewType(4);
				detailBean.setOrderCode(entry.getKey()); 
				detailBean.setCount(count+"");
				detailBean.setPrice(entry.getValue().getPrice());
				detailBean.setPostPrice(""+entry.getValue().getPostPrice());
				detailBean.setDiscountRadio(""+entry.getValue().getDiscountMoney());
				//设置订单ID
				detailBean.setId(entry.getValue().getId());
				dataList.add(detailBean);
				
			}//采用交易关闭界面
			else if(paystatus==0&&orderstatus==8)
			{  
				InnerDetailBean detailBean=new InnerDetailBean();
				detailBean.setViewType(5);
				detailBean.setOrderCode(entry.getKey()); 
				detailBean.setCount(count+"");
				detailBean.setPrice(entry.getValue().getPrice());
				detailBean.setPostPrice(""+entry.getValue().getPostPrice());
				detailBean.setDiscountRadio(""+entry.getValue().getDiscountMoney());
				//设置订单ID
				detailBean.setId(entry.getValue().getId());
				dataList.add(detailBean);
			}
			
			}
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Set<Entry<String, DetailBean>> set;
		if (map == null) {
			return;
		}else
		{
		    set = map.entrySet();
		  if (set == null) {
			 return;
		  }
		}

		

		for (Entry<String, DetailBean> entry : set) {

			if (entry == null || entry.getValue() == null || entry.getKey() == null) {
				continue;
			}
//			DebugLog.v("ordercode", entry.getValue().getOrderCode());
//			DebugLog.v("ordercode1", dataList.get(position - 1).getOrderCode());
			if(position!=0)
			{
			if (dataList.get(position - 1).getOrderCode().equals(entry.getValue().getOrderCode())) {
				
				
				
				 String goodsid=entry.getValue().getId();
				//Toast.makeText(App.ctx, "haha", 0).show();
				 List<InnerDetailBean> listInner = entry.getValue().get_items();
				// DebugToast.show(listInner.size(), 1);
				 Bundle bundle=new Bundle();
				 bundle.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) listInner);
				 
				    int paystatus=Integer.parseInt(entry.getValue().getPayStatus());
					int orderstatus=Integer.parseInt(entry.getValue().getOrderStatus());
				 
				    Intent intent=null;
				    //采用待发货界面
					if((paystatus==1&&orderstatus==3)||(paystatus==1&&orderstatus==2))
					{
						intent = new Intent(getActivity(), SendActivity.class);
					}
			
					//采用待付款界面
					else if(paystatus==0&&orderstatus==2)
					{
						intent = new Intent(getActivity(), PayActivity.class);
					}
					//采用待收货界面
					else if(paystatus==1&&orderstatus==4)
				   	{
						intent = new Intent(getActivity(), ReceiveActivity.class);
				   	}
					//采用已完成界面
					else if(paystatus==1&&orderstatus==7||paystatus==1&&orderstatus==5)
					{  
						intent = new Intent(getActivity(), CompleteActivity.class);
					}else if(paystatus==0&&orderstatus==8)
					{
						//intent = new Intent(getActivity(), CompleteActivity.class);
					}
					
				 
				 if(intent!=null)
				 {
				  intent.putExtras(bundle);
		//		  DebugToast.show(entry.getValue().getOrderCode(), 0);
				  intent.putExtra("goodsid", goodsid);
				  intent.putExtra("what", dataList.get(position - 1).getId());
				  startActivity(intent);
				 }
			}
			}
		}
		
	}

					 
}
